package com.dqhl.community.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.arrownock.im.callback.AnIMBinaryCallbackData;
import com.arrownock.im.callback.AnIMCallbackAdapter;
import com.arrownock.im.callback.AnIMMessageCallbackData;
import com.arrownock.im.callback.AnIMMessageSentCallbackData;
import com.arrownock.im.callback.AnIMReadACKCallbackData;
import com.arrownock.im.callback.AnIMReceiveACKCallbackData;
import com.arrownock.im.callback.AnIMStatusUpdateCallbackData;
import com.dqhl.community.model.ChatBean;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by LIENLIN on 2016/9/8.
 * 继承 AnIMCallbackAdapter 抽象类来实现回调类
 */
public class MessageCallback extends AnIMCallbackAdapter {

    private ChatActivity activity;
    public MessageCallback(ChatActivity activity) {
        this.activity=activity;
    }

    // 接收文本消息
    @Override
    public void receivedMessage(AnIMMessageCallbackData data) {
        String msgId = data.getMsgId();
        final String from = data.getFrom();
        Set<String> parties = data.getParties();
        final String message = data.getMessage();
        Map<String, String> customData = data.getCustomData();
        Log.d("LOG_TAG", "收到私聊消息: " + message);

        //将消息传回聊天界面
        ChatBean chat=new ChatBean(msgId,message,null,null,null);

        activity.getMsg(chat);

        if (customData != null) {
            Set<String> keys = customData.keySet();
            Iterator<String> it = keys.iterator();
            while (it.hasNext()) {
                String key = it.next();
                String status = customData.get(key);
                Log.d("LOG_TAG", key + " : " + status);
            }
        }
    }

    //接收二进制资源
    @Override
    public void receivedBinary(AnIMBinaryCallbackData anIMBinaryCallbackData) {
        super.receivedBinary(anIMBinaryCallbackData);
        String fileType = anIMBinaryCallbackData.getFileType();//类型
        byte[] content = anIMBinaryCallbackData.getContent();//二进制
        Bitmap bitmap = BitmapFactory.decodeByteArray(content, 0, content.length);
        Log.d("LOG_TAG", " fileType= " + fileType);
        ChatBean chat=new ChatBean(null,null,content,fileType,null);
        activity.getImg(chat);
    }

    // 已送达
    @Override
    public void receivedReceiveACK(AnIMReceiveACKCallbackData data) {
        String msgId = data.getMsgId();
        String from = data.getFrom();
        Log.d("LOG_TAG", "message: " + msgId + " recevied by: " + from);
    }

    //已发送
    @Override
    public void messageSent(AnIMMessageSentCallbackData anIMMessageSentCallbackData) {
        super.messageSent(anIMMessageSentCallbackData);
        String msgId = anIMMessageSentCallbackData.getMsgId();

        Log.d("MessageCallback","已发送"+msgId);
    }

    //已阅读
    @Override
    public void receivedReadACK(AnIMReadACKCallbackData anIMReadACKCallbackData) {
        super.receivedReadACK(anIMReadACKCallbackData);
        String msgId = anIMReadACKCallbackData.getMsgId();
        Log.d("MessageCallback","已阅读"+msgId);
    }

    @Override
    public void statusUpdate(AnIMStatusUpdateCallbackData anIMStatusUpdateCallbackData) {
        super.statusUpdate(anIMStatusUpdateCallbackData);

        String s = anIMStatusUpdateCallbackData.getStatus().name();
        Log.d("MessageCallback","登录状态"+s);
    }
}
